<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var person = {
            name: 'sam',
            age: 20
        }

        // 基本解构
        var { name, age } = person;
        console.log(name, age);//sam 20

        var { name, age } = { name: 'sam', age: 20 };
        console.log(name, age);//sam 20

        var { name3: age } = { name3: 'zhang' };
        // console.log(name3);//报错
        console.log(age);//zhang

        // 嵌套
        var obj = { id: ['admin', { sex: '男' }] };
        var { id: [name1, { sex }] } = obj;
        // console.log(id);//报错
        console.log(name1);//admin
        console.log(sex);//男

        // 剩余运算符
        var { a, b, ...rest } = { a: 10, b: 20, c: 30, d: 40 };
        // a = 10
        // b = 20
        // rest = {c: 30, d: 40}

        //默认值
        var { a = 10, b = 5 } = { a: 3 };
        // a = 3; b = 5;
        var { a: aa = 10, b: bb = 5 } = { a: 3 };
        // aa = 3; bb = 5;
    </script>
</body>

</html>